$(function(){
	var thisUrl=document.URL;
	var getVal=thisUrl.split('?')[1];
	var bookId=(getVal.split('&')[0]).split('=')[1];
	var status=(getVal.split('&')[1]).split('=')[1];
	
//	$.ajax({
//		data:{"BookId":bookId},
//		type:"POST",
//		url:"GetOneBookServlet",
//		dataType:"json",
//		suncess:function(response){
//			console.log(response);
//			
//	});
	
	$.post("GetOneBookServlet",{"BookId":bookId},function(response){
		$("#bookD").append("<div id='Book'>\
            <div id='book-pic'>\
            <img src='"+response.Picture+"'>\
        </div>\
        <div id='book-detail'>\
            <div id='book-title'>"+response.BookName+"</div>\
            <h3>"+response.Author+"</h3>\
            <div id='book-price'>\
                <div class='price-icon'>$</div>\
                <div class='price'><span>"+response.Price+"</span></div>\
            </div>\
            <div id='book-stock'>\
                <div class='stock-name'>stock:</div>\
                <div class='stock-number'><span>"+response.Stock+"</span></div>\
            </div>\
            <div id='book-buy'>\
                <div class='book-btn'><a id='btnpay' href='InsertOrderByBookIdServlet?bookId="+response.BookId+"'>Buy</a></div>\
                <div class='book-btn'><a id='btnadd' onclick='addToCart("+response.BookId+")'>Add To Cart</a></div>\
            </div>\
        </div>\
    </div>\
        <div id='book-direction'>\
            <div id='direction-title'>Description:</div>\
            <div id='direction-content'>\
            "+response.Description+"\
                </div>\
        </div>")
        
        if(status=="aaa1254"){
       	 $("#btnadd").attr('href',"javascript:void(0);");
       	 $("#btnadd").removeAttr("onclick");
       	$("#btnadd").click(function(){
      		 alert("请先登录");
      	 });
       	
       	 $("#btnpay").attr('href',"javascript:void(0);");
       	 $("#btnpay").click(function(){
       		 alert("请先登录");
       	 });
          }
		},"json");
	
	$.ajax({	
		data:{"bookId":bookId},
		type:"POST",
		url:"getCommentByBookIdServlet",
		dataType:"json",
		success:function(response){
			$("#comment").append("<div id='comment-title'>Comment</div><div id='comment-rev'></div>");
			$(response).each(function(index,elem){
				$("#comment-rev").append("\
                    <div class='comment-item'>\
                        <div class='item-whom'>\
                            <div class='item-name'>"+elem.customer.UserName+":</div>\
                        </div>\
                        <div class='item-review'>\
                            <div class='item-text'>"+elem.CommentContent+"</div>\
                            <div class='item-date'>"+elem.CommentTime+"</div>\
                        </div>\
                    </div>\
                    ");			
			});
		}
	})
})

function addToCart(BookId){
	$.ajax({
		data:{
			"bookId":BookId,
			"quantity":"1"
		},
		type:"post",
		url:"AddCartServlet",
		success:function(response){
			if(response=="success")
			{
				alert("Addition succeeded!");
			}
			else{
				alert("Addition failed!");
			}
			
			$.ajax({
		    	url: 'CountGoodsInCartServlet',
		    	type: 'POST',
		    	success: function(response) {
		    		$('#itemCount').text(response);
		    	}
		    });
		}
	});
  }